#include <cstdlib>
#include <iostream> 
#include "rational.h"

Rational::Rational( int n, int d )
{
   if (d)
   {
		numer = n;
		denom = d;
   }
   else
   {
		numer = 1;
		denom = 1;
   }
}

//This function is used to stop the client's while loop, that's ALL!!
bool Rational::operator!=(int pnum )
   { return !( numer == pnum ); }

//Don't be too concerned with how this function works.
void Rational::reduction()
{
   int largest, gcd = 1;  // greatest common divisor;

   largest = ( numer > denom ) ? numer: denom;

   for ( int loop = 2; loop <= largest; ++loop )
       if ( numer % loop == 0 && denom % loop == 0 )
          gcd = loop;

   numer /= gcd;
   denom /= gcd;
}

Rational Rational::operator +(Rational Robj)
{
	Rational tempObj;

	tempObj.denom = denom * Robj.denom;
	tempObj.numer = (denom * Robj.numer) + (numer * Robj.denom);

	tempObj.reduction();

	return tempObj;
}

Rational Rational::operator -(Rational Robj)
{
	Rational tempObj;

	tempObj.denom = denom * Robj.denom;
	tempObj.numer = (denom * Robj.numer) - (numer * Robj.denom);

	tempObj.reduction();

	return tempObj;
}

Rational Rational::operator *(Rational Robj)
{
	Rational tempObj;

	tempObj.denom = denom * Robj.denom;
	tempObj.numer = Robj.numer + numer;

	tempObj.reduction();

	return tempObj;
}

Rational Rational::operator /(Rational Robj)
{
	Rational tempObj;

	tempObj.denom = denom * Robj.numer;
	tempObj.numer = Robj.denom * numer;

	tempObj.reduction();

	return tempObj;
}

bool Rational::operator <(Rational Robj)
{
	float tempLeft = numer / denom;
	float tempRight = Robj.numer / Robj.denom;

	if(tempLeft > tempRight)
		return false;
	else
		return true;
}

bool Rational::operator >(Rational Robj)
{
	float tempLeft = float(numer) / denom;
	float tempRight = float(Robj.numer) / Robj.denom;

	if(tempLeft < tempRight)
		return false;
	else
		return true;
}

ostream &operator <<(ostream &output, Rational &Robj)
{
	output << Robj.numer << "/" << Robj.denom << endl;

	return output;
}

istream &operator>>(istream &input, Rational &Robj)
{
	char slash;

	input >> Robj.numer >> slash >> Robj.denom;

	return input;
}















